﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using IndexMarkSilverlightClient.Classes;
using IndexMarkSilverlightClient.IndexMarkServiceReference;

namespace IndexMarkSilverlightClient.Views.Controls.ReportsControls
{
    public partial class RaspredelenieNadbavok : UserControl
    {

        IndexMarkServiceReference.IndexMarkServiceClient client = new IndexMarkServiceClient();

        private ObservableCollection<Money> _money = new ObservableCollection<Money>();
        private int _termDI = 6;


        public RaspredelenieNadbavok()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(RaspredelenieNadbavok_Loaded);
        }

        void RaspredelenieNadbavok_Loaded(object sender, RoutedEventArgs e)
        {
            //var vm = (((((((this.Parent as Grid).Parent as Grid).Parent as ReportsMain).Parent as Frame).Parent as Border).Parent as Grid).Parent as MainPage).ViewModel;
            var vm = MainPage.ViewModel;
            vm.SelectTerm -= ViewModel_SelectTerm;
            vm.SelectTerm += new MainViewModel.SelectTermEventHandler(ViewModel_SelectTerm);
            vm.SelectTermInit();
            client.GetPremiumsCompleted += (_s, _e) =>
            {
                _money = _e.Result;
                DrawDiagramm();
            };
        }

        void ViewModel_SelectTerm(object sender, SelectTermEventsArgs e)
        {
            _termDI = e.SelectTerm.IDTerm;
            //_termDI = 6;
        }

        void DrawDiagramm()
        {
            var IndexesMain =
                (from mon in _money
                 group mon by new { Pos = mon.Resp.Position }
                     into IndGroup
                     select new KeyValuePair<string, double>(IndGroup.Key.Pos,
                         (double)IndGroup.Sum(x => x.Premium))).ToList();
        }
    }
}